// 确认订单 
<template>
  <div class="ConfirmationOfOrder" v-loading.fullscreen.lock="fullscreenLoading">
    <nav-login></nav-login>
    <div class="w1200">
      <div class="cartStep">
        <cart-logo></cart-logo>
        <cart-step :steps="steps"></cart-step>
      </div>
      <receiving-address @addressChanges="addressChanges"></receiving-address>
      <detailed-list @formChanges="formChanges"></detailed-list>
      <payment-method :elbG="elbG" :title1="title1" :active="1"></payment-method>
      <!-- <payment-method :elbG="DeliveryTime"  :title1="title2" :active="1"></payment-method> -->
      <!--送达时间-->
      <invoice-info
        @InvoiceInfoIDChange="InvoiceInfoIDChange"
        :DefaultAddress="defaultAddress"
        :changesUser="changesUser"
        :ReceivingAddressID="ReceivingAddressID"
      ></invoice-info>
      <!-- <confirn-coupon></confirn-coupon> -->
      <!--优惠券-->
      <total-price
        :defaultAddress="defaultAddress"
        :changesUser="changesUser"
        @placeOrder="placeOrder"
        @TotalPriceData="TotalPriceData"
      ></total-price>
    </div>

    <bottom-nav></bottom-nav>
    <home-footer></home-footer>
  </div>
</template>
<script>
import cartStep from "../shoppingCart/components/CartStep"; // 步骤
import cartLogo from "../shoppingCart/components/Logo"; // logo
import receivingAddress from "./components/ReceivingAddress"; // 收货地址
import detailedList from "./components/DetailedList"; // 商品清单
import paymentMethod from "./components/PaymentMethod"; // 支付方式
import invoiceInfo from "./components/InvoiceInfo"; // 发票信息  发票收货地址
import confirnCoupon from "./components/Coupon"; // 可用优惠劵 折扣
import totalPrice from "./components/TotalPrice"; // 总计花费金额
export default {
  name: "confirmOrder",

  data() {
    return {
      freight: null,
      fullscreenLoading: false,
      contractNum: "", // 合同号
      Remarks: "", //备注的内容
      ReceivingAddressID: "", // 收货地址ID
      InvoiceAddressID: "", // 发票地址ID
      TotalPaidPrice: 0, //  减掉邮费减掉优惠的价格
      steps: 2,
      title1: "支付方式",
      elbG: ["账期", "预付"],
      title2: "送达时间",
      DeliveryTime: ["任意时间", "工作日送货", "非工作日送货"],
      tableData: [],
      defaultAddress: "", // 收货地址选择
      changesUser: "" // 收货人姓名
    };
  },

  components: {
    cartStep,
    cartLogo,
    receivingAddress,
    detailedList,
    paymentMethod,
    invoiceInfo,
    confirnCoupon,
    totalPrice
  },
  methods: {
    // 子組件激发的父组件传值 获取总价
    TotalPriceData(TotalPaidPrice, freight) {
      this.TotalPaidPrice = TotalPaidPrice;
      this.freight = freight;
    },

    // 合同号和备注激发的函数
    formChanges(form) {
      this.contractNum = form.contractNum;
      this.Remarks = form.Remarks;
      console.log(this.contractNum, this.Remarks);
    },

    // 点击提交订单的按钮激发父组件的函数
    async placeOrder() {
      let freight_price = this.freight; // 运费
      if(freight_price === null){
        this.$notify.error({
          title: "提示",
          message: "运费计算出现错误,可能由于网络原因请稍后再试,或在线联系客服..."
        });
        return false;
      }
      let user_id = this.$store.state.UserInfor.user_id; //用户号
      let receipt_address_id = this.ReceivingAddressID; // 收货地址ID
      let invoice_address_id = this.InvoiceAddressID; // 发票地址ID
      let contract_no = this.contractNum; //合同号
      let remark = this.Remarks; // 备注
      let goods_price = this.TotalPaidPrice; // 订单总价格
      let id_card = this.$store.state.id_card; // 身份证号
      let invoice_rise = this.$store.state.invoiceRise; // 发票抬头
      let invoice_type = this.$store.state.InvoiceInfoBtn; // 发票状态 0 或 1 或 2   *****   0为不开发票  1 为普通 2 为专业
      let param = [];

      this.$store.state.SettlementData.forEach((element, index) => {
        // 商品的详细信息 找到有用数据
        param.push({
          id: element.id,
          num: element.num
        });
      });

      console.log(
        freight_price,
        user_id,
        receipt_address_id,
        invoice_address_id,
        contract_no,
        remark,
        goods_price,
        id_card,
        invoice_rise,
        invoice_type,
        param
      );
      console.log(this.$store.state.InvoiceInfoBtn);
      if (receipt_address_id) {
        this.fullscreenLoading = true; // 遮罩
        let flag = this.$store.state.InvoiceInfoBtn;
        console.log(flag);
        if (flag == 1) {
          // 普通发票
          if (invoice_address_id) {
            // 判断收货地址
            let res = await this.$http({
              method: "post",
              url: "/api/index/api/createOrder",
              data: {
                freight_price,
                user_id,
                receipt_address_id,
                invoice_address_id,
                contract_no,
                remark,
                goods_price,
                id_card,
                invoice_rise,
                invoice_type,
                param
              }
            });
            if (res.data.State.Code == 200) {
              console.log(res.data);
              this.$store.commit("order_no", res.data.order_no); // 把订单号存储在vuex中
              this.$router.push({
                name: "payment",
                params: {}
              });
              this.fullscreenLoading = false;
            }
          } else {
            this.fullscreenLoading = false;
            this.$notify.info({
              title: "请填写发票地址..."
            });
            return;
          }
        } else if (flag == 2) {
          // 专业发票
          if (invoice_address_id) {
            // 判断收货地址
            let res = await this.$http({
              method: "post",
              url: "/api/index/api/createOrder",
              data: {
                freight_price,
                user_id,
                receipt_address_id,
                invoice_address_id,
                contract_no,
                remark,
                goods_price,
                id_card: "",
                invoice_rise,
                invoice_type,
                param
              }
            });
            if (res.data.State.Code == 200) {
              console.log(res.data);
              this.$store.commit("order_no", res.data.order_no); // 把订单号存储在vuex中
              this.$router.push({
                name: "payment",
                params: {}
              });
            }
          } else {
            this.fullscreenLoading = false;
            this.$notify.info({
              title: "请填写发票地址..."
            });
            return;
          }
        } else {
          // 不开发票
          let res = await this.$http({
            method: "post",
            url: "/api/index/api/createOrder",
            data: {
              freight_price,
              user_id,
              receipt_address_id,
              invoice_address_id: "",
              contract_no,
              remark,
              goods_price,
              id_card: "",
              invoice_rise: "",
              invoice_type,
              param
            }
          });
          if (res.data.State.Code == 200) {
            console.log(res.data);
            this.$store.commit("order_no", res.data.order_no); // 把订单号存储在vuex中
            this.$router.push({
              name: "payment",
              params: {}
            });
            this.fullscreenLoading = false;
          }
        }
      } else {
        this.$notify.error({
          title: "提示",
          message: "请填写收货地址..."
        });
      }
    },
    // 发票地址接收ID
    InvoiceInfoIDChange(id) {
      this.InvoiceAddressID = id;
    },
    // 收货地址激发的父组件的函数
    addressChanges(defaultAddress, changesUser, ReceivingAddress) {
      this.defaultAddress = defaultAddress;
      this.changesUser = changesUser;
      this.ReceivingAddressID = ReceivingAddress;
      this.getFreight();
    },

    // 获取运费
    async getFreight() {
      let province_id = this.$store.state.changesAddress.province_id; // 省份id
      let goods_info = [];
      let goods = this.$store.state.SettlementData;

      goods.forEach((item, index) => {
        goods_info.push({ id: item.id, province_id, num: item.num });
      });
      try {
        let res = await this.$http({
          method: "post",
          url: "/api/home/settlement/getexpress",
          data: {
            goods_info
          }
        });

        if (res.data.State.Code == 200) {
          this.freight = +res.data.price.toFixed(2);
          this.$store.commit("getFreight", this.freight);
        } else {
          this.$notify.error({
            title: "提示",
            message: "运费计算出现错误,可能由于网络原因请稍后再试,或在线联系客服..."
          });
        }
      } catch (error) {
        this.$notify.error({
          title: "提示",
          message: "运费计算出现错误,可能由于网络原因请稍后再试,或在线联系客服..."
        });
      }
    }
  },

  watch: {
    ReceivingAddressID(val) {
      this.getFreight();
    }
  }
};
</script>
<style lang="scss" scoped>
.w1200 {
  width: 1200px;
  margin: auto;
  .cartStep {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }
  .Tips {
    line-height: 48px;
    border: 1px solid #f3f3f3;
    color: #2d2d2d;
    text-indent: 34px;
    font-size: 14px;
  }
}
</style>
